//
//  EJSSegmentedControl.m
//  EnjoyShoppingStreet
//
//  Created by lifayu on 2017/12/14.
//  Copyright © 2017年 lifayu. All rights reserved.
//

#import "EJSSegmentedControl.h"
#import "EJSSegmentedControlCollectionViewCell.h"

@interface EJSSegmentedControl()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UICollectionView *collectionView;

@end

@implementation EJSSegmentedControl

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if(self)
    {
        [self addOwnViews];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if(self)
    {
        [self addOwnViews];
    }
    return self;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    self.collectionView.frame = self.bounds;
}


- (void)addOwnViews
{
    self.clipsToBounds = NO;
    self.backgroundColor = UIColorFromRGB(0x3C4151);
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    layout.itemSize = CGSizeMake(58, 50);
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    self.collectionView.backgroundColor = kClearColor;
    self.collectionView.clipsToBounds = NO;
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.showsVerticalScrollIndicator = NO;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.bounces = true;
    [self.collectionView registerNib:[UINib nibWithNibName:@"EJSSegmentedControlCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"EJSSegmentedControlCollectionViewCell"];
    
    [self addSubview:self.collectionView];
}

- (void)setCategoryTitles:(NSArray *)categoryTitles
{
    if(_categoryTitles != categoryTitles)
    {
        _categoryTitles = categoryTitles;
        [self.collectionView reloadData];
    }
}

- (void)setSelectedIndex:(NSUInteger)selectedIndex
{
    if(selectedIndex >= _categoryTitles.count)
    {
        return;
    }
    _selectedIndex = selectedIndex;
    if(_selectedIndex < _categoryTitles.count)
    {
        [self.collectionView selectItemAtIndexPath:[NSIndexPath indexPathForItem:_selectedIndex inSection:0] animated:YES scrollPosition:UICollectionViewScrollPositionCenteredHorizontally];
    }
}

#pragma mark CollectionView Delegate\DataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _categoryTitles.count;
}

- (UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
    EJSSegmentedControlCollectionViewCell *ccell = [collectionView dequeueReusableCellWithReuseIdentifier:@"EJSSegmentedControlCollectionViewCell" forIndexPath:indexPath];
    ccell.titleLabel.text = _categoryTitles[indexPath.item];
    return ccell;
}

- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    return YES;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if(self.selectedIndex != indexPath.item)
    {
        self.selectedIndex = indexPath.item;
        if(_delegate && [_delegate respondsToSelector:@selector(segmentedControl:selectItemAtIndex:)])
        {
            [_delegate segmentedControl:self selectItemAtIndex:self.selectedIndex];
        }
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    NSString *title = _categoryTitles[indexPath.item];
    
    CGSize textSize = [title sizeWithFont:[UIFont systemFontOfSize:14.0] maxSize:CGSizeMake(ScreenWidth, collectionView.height)];
    CGFloat width = textSize.width + 30;
    if(width < 58){
        width = 58;
    }
    return CGSizeMake(width, collectionView.height);
}



@end
